/*
* @Author: zlx
* @Date:   2018-02-27 14:08:24
* @Last Modified by:   zlx
* @Last Modified time: 2018-02-27 16:21:03
*/
//图形验证唯一标识 flag ：图形验证结果 true为真 flase 为假 type：登录/注册 短信验证类型：1为登录、忘记密码  2为注册
var codeid,flag=false,type=1,time=60;
$(function(){
	var phone='';
	//验证手机号
	var panter=/^1[34578][0-9]{9}$/;
	$(document).on('click','.send',function(){
		if($(this).attr('id')){
			type=$(this).attr('type');
			if(!panter.test($('.phone_n').val())){
				tips('请输入正确的手机号');
				return false;
			}else{
				$(".cont").css("display","block");
		    	getImg();
			}
		}
	});
	$(document).on('click','#shanchu',function(){
		//关闭状态重置
	    $(".cont").css("display","none");
	    $('.xiao').css({'left':'0'})
	    $('#div1').css({'left':'0'})
	});
	//获取图形验证图片
	pcMove();
	appMove();
	//验证成功发送验证码
	if(flag){
		
	}else{
		return false;
	}
	
})
//获取验证图片
function getImg(){
//	$.get('http://123.206.92.142:20031/mobile/view/checkimagecount/hdimage/1234',{//测试地址
//	uniquenessCode:'123'
	$.post(apicom+'mobile/view/member/getImage',{
		uniquenessCode:'login'
	},function(res){
		res=JSON.parse(res);
		console.log(res)
		if(res.code==1){
			$('#back').css({'background':'url("'+res.content.smallUrl+'")','backgroundSize':'100% 100%'});
			$('.xiao').attr('src',res.content.templeUrl);
			codeid=res.content.uniqueCode;
		}
	})
}
//验证结果
function yan(left){
	$.post(apicom+'mobile/view/member/checkImage',{
		uniquenessCode:codeid,
		x:left,
//		type:'123'
	},function(res){
		res=JSON.parse(res)
		console.log(res)
		tips(res.msg)
		if(res.code=='1'){
			flag=true;
			//图形验证成功 发送验证码
			$.post(apicom+'mobile/view/member/sendSms',{
				phone:$('.phone_n').val(),
				type:type
			},function(res){
				res=JSON.parse(res)
				setTimeout(function(){
					tips(res.msg)
				},1000);
				$('.send').attr('id','');
                $('.send').text("重新获取" + "(" + (time) + ")");
                var setId = setInterval(function() {
                    if (time <= 0) {
                        $('.send').attr('id','send');
                        $('.send').text("发送验证码");
                        clearInterval(setId);
                        time=60;
                    } else {
                        time--;
                        $('.send').text("重新获取" + "(" + (time) + ")");
                    }
                }, 1000);
			})
		}else{
			flag=false;
		}
		$('.xiao').css({'left':'0'})
	    $('#div1').css({'left':'0'})
		$(".cont").css("display","none");
	})
}
//pc端滑动验证
function pcMove(){
	//小滑块
	var div1 = document.getElementById("div1");
	//大图片
	var div2 = document.getElementById("tiao");
	//小图片
	var img = document.getElementById("xiao");
	div1.onmousedown = function(ev){
　　　	var oevent = ev || event;

　　　	var distanceX = oevent.clientX - div1.offsetLeft;
　　 		var distanceY = oevent.clientY - div1.offsetTop;
		var left;
　　 		document.onmousemove = function(ev){
　　　　　　	var oevent = ev || event;
			left=oevent.clientX - distanceX;
			if(left<0){
				left=0;
			}else if(left>div2.offsetWidth-div1.offsetWidth){
				left= div2.offsetWidth-div1.offsetWidth;
			}
			div1.style.left = left + 'px';
			img.style.left = left + "px";
//　　　　　		div1.style.top = oevent.clientY - distanceY + 'px'; 
　　　　  };
　　　　  document.onmouseup = function(){
　　　　　　	document.onmousemove = null;
　　　　　　	document.onmouseup = null;
			//验证图片
			var part=parseInt(Math.round((left/div2.offsetWidth*500)*100)/100);
			yan(part)
			console.log(left);
　　　	};
	};
}
//web端滑动验证
function appMove(){
	//小滑块
	var div1 = document.getElementById("div1");// 获取节点
	//大图片
	var div2 = document.getElementById("tiao");
	//小图片 
	var img = document.getElementById("xiao");
	var oW,oH;
	//绑定touchstart事件
	div1.addEventListener("touchstart", function(e) {
		console.log(e);
		var touches = e.touches[0];
		oW = touches.clientX - div1.offsetLeft;
		oH = touches.clientY - div1.offsetTop;
		//阻止页面的滑动默认事件
    	document.addEventListener("touchmove",defaultEvent,false);
    },false)
	var left;
	div1.addEventListener("touchmove", function(e) {
		var touches = e.touches[0];
    	var oLeft = touches.clientX - oW;
    	// var oTop = touches.clientY - oH;
    	if(oLeft < 0) {
    		oLeft = 0;
    	}else if(oLeft >div2.offsetWidth-div1.offsetWidth) {
    		oLeft = div2.offsetWidth-div1.offsetWidth;
    	}
    	left=oLeft;
    	div1.style.left = oLeft + "px";
    	img.style.left = oLeft + "px";
    	// div1.style.top = oTop + "px";
    },false);
    div1.addEventListener("touchend",function() {	
    	document.removeEventListener("touchmove",defaultEvent,false);
    	var part=parseInt(Math.round((left/div2.offsetWidth*500)*100)/100);
    	yan(part);
    	console.log(div2.offsetWidth,part);
    },false);
    function defaultEvent(e) {
    	e.preventDefault();
    }
}